package com.myapp.web.struts2.components;

import java.io.CharArrayWriter;
import java.io.Writer;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsException;
import org.apache.struts2.components.ClosingUIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

@StrutsTag(name="segment", tldTagClass="com.etrade.web.jsp.ui.SegmentTag", description="the segment of the tag")  
public class SegmentBean extends ClosingUIBean {
	
    private static final Logger LOG = LoggerFactory.getLogger(SegmentBean.class);  
    
    protected StringBuffer embodyContent; 
    
    private String prefixClass;
      
    public String getEmbodyContent(){
    	if(embodyContent != null){
    		return embodyContent.toString();
    	}else{
    		return "";
    	}
    }
    
    private void buildEmbodyContent(){
    	if(embodyContent == null){
    		embodyContent = new StringBuffer();
    	}
    }
    
	public SegmentBean(ValueStack stack, HttpServletRequest request,HttpServletResponse response) {
		 super(stack, request, response);
	}
	
	public boolean usesBody() {
	    return true;
	}
	
	public boolean start(Writer writer) {
		CharArrayWriter w = new CharArrayWriter();
        //boolean result = super.start(w);
		boolean result = true;
        buildEmbodyContent();
        try {
            evaluateParams();            
            mergeTemplate(w, buildTemplateName(getDefaultOpenTemplate(), getDefaultOpenTemplate()));
            embodyContent.append(w.toCharArray());
        } catch (Exception e) {
            LOG.error("Could not open template", e);
            e.printStackTrace();
        }
        return result;
    }
	
	public void putEmbody(String content){
		Map<String,Object> map = stack.getContext();
		Integer index = (Integer)map.get(ContainerUIBean.SIZE);
		map.put(ContainerUIBean.KEY_PREFIX + index, content);
		map.put(ContainerUIBean.SIZE, index + 1);		
        
	}
	
	public boolean end(Writer writer, String body) {
        //super.evaluateParams();
        CharArrayWriter w = new CharArrayWriter();
        try {     
            super.end(w, body, false);
            mergeTemplate(w, buildTemplateName(getDefaultTemplate(), getDefaultTemplate()));
            embodyContent.append(w.toCharArray());
            putEmbody(getEmbodyContent());
        } catch (Exception e) {
            throw new StrutsException(e);
        }
        return false;
    }
	
	
	@Override
	protected void evaluateExtraParams() {
		super.evaluateExtraParams();
		if(prefixClass != null && !"".equals(prefixClass)){
			addParameter("prefixClass", prefixClass);
		}
		
		final ContainerUIBean containerUIBean = (ContainerUIBean)findAncestor(ContainerUIBean.class);

        if (containerUIBean != null ) {
            addParameter("container", containerUIBean.getParameters());
        }        
	}

	@Override
	protected String getDefaultTemplate() {
		return template.concat("-close");
	}	

    public String getDefaultOpenTemplate() {
        return template;
    }

    @StrutsTagAttribute(description="set the prefix of class property of the sytle")
	public void setPrefixClass(String prefixClass) {
		this.prefixClass = prefixClass;
	}
}
